var Joinmeeting = function() {


    // 参会人员
    var joinmeetingDatalist_url = $('.joinmeetingDatalist_url').val();
    var joinmeetingDelete_url = $('.joinmeetingDelete_url').val();
    var joinmeetingAdd_url = $('.joinmeetingAdd_url').val();
    var joinmeetingdetail_url = $('.joinmeetingdetail_url').val();

    var memberDatalist_url = $('.memberDatalist_url').val();

    var isLoadJoinmeetingTable = isLoadMemberTable = false;


    var initJoinmeetingTable = function() {
        var table = $('#joinmeeting-datatable');
        var meetingId = $('.meetingId').val();

        if(table.length > 0){
            table.dataTable({
                "processing": true,
                "ordering": false, // 禁止排序
                serverSide: true,// 开启服务器模式
                "ajax": {
                    url: joinmeetingDatalist_url,
                    data: {'id':meetingId}
                },
                "autoWidth": false,
                "columns": [
                    {
                        "data": "p_imgUrl",
                        "render": function(data, type, row, meta) {
                            return '<img src="' + data + '" style="width: 40px;height: 40px;"/>';
                        }
                    },
                    {"data": "p_name"},
                    {"data": "duties"},
                    {
                        "width": "15%",
                        "data": "null",
                        "render": function(data, type, row, meta) {
                            var html = '<button type="button" class="btn btn-danger delete-btn">删除</button>';

                            return html;
                        }
                    }
                ],
                "language": {
                    url: '/public/static/admin/pages/datatable_cn.json'
                },
                "lengthMenu": [[10, 20, 50, 100, 150], [10, 20, 50, 100, 150]],
                "pageLength": 10,
                "scrollX":"",
                "destroy": true, // 重新加载表格内容
                "columnDefs": [{
                    "orderable": false,
                }],
                "dom": "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>" +
                "<t>" +
                "<'relative'<'col-md-5'i><'col-md-7'>lp>",
                "fnCreatedRow": function(nRow, aData, iDataIndex){
                    //
                },
                "fnInitComplete": function() {
                    //var nTrs = table.fnGetNodes();//fnGetNodes获取表格所有行，nTrs[i]表示第i行tr对象
                }
            });

            isLoadJoinmeetingTable = true;
        }

    };


    var initMemberTable = function() {
        var table = $('#member-datatable');

        if(table.length > 0){
            table.dataTable({
                "processing": true,
                "ordering": false, // 禁止排序
                serverSide: true,// 开启服务器模式
                "ajax": {
                    url: memberDatalist_url,
                    data: {}
                },
                "autoWidth": false,
                "columns": [
                    {
                        "data": "p_imgUrl",
                        "render": function(data, type, row, meta) {
                            return '<img src="'+data+'" style="width: 40px;height: 40px;"/>';
                        }
                    },
                    {"data": "p_name"},
                    {"data": "duties"},
                    {
                        "width": "15%",
                        "data": "null",
                        "render": function(data, type, row, meta) {
                            var html = '<button type="button" class="btn btn-success select-btn">选择</button>';

                            return html;
                        }
                    }
                ],
                "language": {
                    url: '/public/static/admin/pages/datatable_cn.json'
                },
                "lengthMenu": [[10, 20, 50, 100, 150], [10, 20, 50, 100, 150]],
                "pageLength": 10,
                "scrollX":"",
                //"destroy": true, // 不重新加载表格内容
                "destroy": false,
                "stateSave": false,
                "stateSaveParams": function (settings, data) {
                    //
                },
                "columnDefs": [{
                    "orderable": false,
                }],
                "dom": "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>" +
                "<t>" +
                "<'relative'<'col-md-5'i><'col-md-7'>lp>",
                "fnCreatedRow": function(nRow, aData, iDataIndex){
                },
                "fnInitComplete": function() {
                    //var nTrs = table.fnGetNodes();//fnGetNodes获取表格所有行，nTrs[i]表示第i行tr对象
                }
            });

            isLoadMemberTable = true;
        }

        // 筛选
        $('#doSearch').on('click', function(event){
            var param = $('#searchForm').serializeObject();

            tableSearch(table, param);
        });
        //选择
        table.on('click', '.select-btn', function(event) {
            // 操作行对象
            var dataArr = table.DataTable().rows($(this).parents("tr")).data();
            var id = dataArr[0].id;
            var name = dataArr[0].p_name;

            $('input[name=user_id]').val(id);
            $('.addMember-btn').empty().html(name);
            $('#view-modal').modal('hide');
        });
    };

    var tableSearch = function(table, params) {

        table.DataTable().search(JSON.stringify(params)).draw();
    };

    var viewMember = function (id = '') {
        $('#view-modal').modal('show');
        if(!isLoadMemberTable){
            initMemberTable();
        }
    };



    return {
        init: function() {
            this.onEvent();
        },

        onEvent: function() {

            $('.join-tab').on('click', function(){
                var meetingId = $('.meetingId').val();
                if(meetingId && !isLoadJoinmeetingTable){
                    initJoinmeetingTable();

                }
            });

            // 添加参会人员
            $('.addMember-btn').on('click', function(){
                viewMember();
            });

        }

    };

}();

jQuery(document).ready(function() {
    Joinmeeting.init();

});